
const log = (...arg) => {
    console.log(...arg)
}
function maybeBase64(str) {
    const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-/=';
    return base64Chars.includes(char);
}
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
function watchProxy(arr, name, openLog) {
    // arr 内存
    // name 名字

    // 获取数据类型
    function get_value_type(value) {
        // 'undefined', 'null', 'boolean', 'string', 'number', 'symbol', 'array', 'object', 'function'
        if (Array.isArray(value)) {
            return "array";
        }
        if (value == null) {
            return "null";
        }
        return typeof value;
    }
    // 打印函数
    function test_print(obj) {
        let type = get_value_type(obj);
        console.log(type)
        if (["undefined", "null", "boolean", "string", "number"].includes(type)) {
            return obj;
        } else if (obj[Symbol.toStringTag]) {
            return obj[Symbol.toStringTag];
        } else if (type == "function") {
            return `function ${obj.name}`;
        } else if (type == "object" || type == "array") {
            // JSON.stringify 会递归处理 TODO 
            let temp = JSON.stringify(obj, function (k, v) {
                if (v && v[Symbol.toStringTag]) {
                    return v[Symbol.toStringTag];
                } else if (v && typeof v == "function") {
                    return `function ${v.name}`;
                } else {
                    return v;
                }
            });
            return temp;
        } else if (type == "symbol") {
            return obj.toString();
        } else {
            // 未预料到的情况
            debugger;
        }
    }

    // 句柄
    let handle = {
        get(target, property, receiver) {
            let result;
            result = Reflect.get(target, property, receiver);
            try {
                if (openLog(property, value)) {
                    let content = test_print(target);
                    log(`${name}|get| 下标: ${property.toString()} 对象内容: ${content}\r\n`);


                }
            } catch (e) {
                debugger;
            }
            return result;
        },
        set(target, property, value, receiver) {
            let result;
            result = Reflect.set(target, property, value, receiver);
            try {
                if (openLog(property, value)) {
                    if (value.indexOf("&") < 0) {
                        log("签名长度=", value.length, ",abogus=", value)
                    } else {
                        log(`${name}|set| 下标: ${property} 新内容: ${value}\r\n`);
                    }
                    // TODO 请求连接试试
                    if (value.startsWith("https://")) {
                        log(value)
                        var xhr = new XMLHttpRequest();
                        xhr.onload = () => {
                            console.log("接口返回结果=", xhr.responseText);
                        };
                        xhr.open("GET", value, true);
                        // TODO 补请求头 or 补环境?
                        xhr.setRequestHeader("Accept", "application/json, text/plain, */*");

                        xhr.send()

                    }
                }
            } catch (e) {
                debugger;
            }
            return result;
        },
    };
    return new Proxy(arr, handle);
}

module.exports = { watchProxy };

